package com.xm.dao.background;

import com.xm.entity.CommodityInsurance;
import com.xm.entity.CommodityInsuranceType;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品保险
 * @author 康文程
 * @date 2024-1-10 上午 11:25
 */
public class CommodityInsuranceDao extends BaseUtil {
    /**
     * 根据commodity_id查询商品保险
     *
     * @param commodityId 商品ID
     * @return 商品保险列表
     */
    public List<CommodityInsurance> getCommodityInsuranceByCommodityId(Integer commodityId) {
        @Language("sql")
        String sql = "SELECT ci.*, cit.insurance_type_title,cit.insurance_type_logo" +
                " FROM commodity_insurance ci " +
                "JOIN commodity_insurance_type cit " +
                "ON ci.insurance_type_id = cit.insurance_type_id WHERE ci.commodity_id = ?";
        ResultSet rs = null;
        List<CommodityInsurance> insuranceList = new ArrayList<>();
        try {
            rs = executeQuery(sql, commodityId);
            while (rs.next()) {
                CommodityInsurance commodityInsurance = new CommodityInsurance();
                commodityInsurance.setInsuranceId(rs.getInt("insurance_id"));
                commodityInsurance.setInsuranceTitle(rs.getString("insurance_title"));
                commodityInsurance.setInsuranceContext(rs.getString("insurance_context"));
                commodityInsurance.setInsurancePreferential(rs.getBigDecimal("insurance_preferential"));
                commodityInsurance.setAddPrice(rs.getBigDecimal("add_price"));
                commodityInsurance.setInsuranceType(
                        new CommodityInsuranceType(
                                rs.getString("insurance_type_title"),
                                rs.getString("insurance_type_logo")));
                insuranceList.add(commodityInsurance);
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询失败", e);
        } finally {
            close(rs);
            close(null);
        }
        return insuranceList;
    }
    /**
     * 新增商品保险
     *
     * @param commodityInsurance 要新增的商品保险对象
     * @return 新增成功的记录数
     */
    public int addCommodityInsurance(CommodityInsurance commodityInsurance) {
        // 使用占位符 "?" 来设置参数，防止 SQL 注入攻击
        @Language("sql")
        String sql = "INSERT INTO commodity_insurance (insurance_title, insurance_context, " +
                " add_price, insurance_type_id, commodity_id) " +
                "VALUES (?, ?, ?, ?, ?)";

        try {
            return executeUpdate(sql,
                    commodityInsurance.getInsuranceTitle(),
                    commodityInsurance.getInsuranceContext(),
                    commodityInsurance.getAddPrice(),
                    commodityInsurance.getInsuranceTypeId(),
                    commodityInsurance.getCommodityId());
        } catch (SQLException e) {
            throw new RuntimeException("新增失败", e);
        }
    }
    /**
     * 根据commodity_id和insurance_id删除商品保险
     *
     * @param commodityId 商品ID
     * @param insuranceId 商品保险ID
     * @return 删除成功的记录数
     */
    public int deleteCommodityInsuranceByCommodityIdAndInsuranceId(Integer commodityId, Integer insuranceId) {
        // 使用占位符 "?" 来设置参数，防止 SQL 注入攻击
        @Language("sql")
        String sql = "DELETE FROM commodity_insurance WHERE commodity_id = ? AND insurance_id = ?";
        try {
            return executeUpdate(sql, commodityId, insuranceId);
        } catch (SQLException e) {
            throw new RuntimeException("删除失败", e);
        }
    }
}
